Grokking JavaScript Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who should take this course?
Course Overview
Introduction to JavaScript
JavaScript Basics
JavaScript - Hello World
JavaScript - Comments
Quiz
JavaScript Variables and Operators
JavaScript - Variables
JavaScript - Operators
Quiz
JavaScript Conditionals
Introduction to Conditional Statements in JavaScript
JavaScript - If...Else
JavaScript - Switch Case
Quiz
JavaScript Objects
Introduction to Objects in JavaScript
JavaScript - Number
JavaScript - Boolean
JavaScript - Math
JavaScript - Date
Quiz
Introductions to Function in JavaScript
JavaScript - Variable Scope
JavaScript - Default Parameters
JavaScript - Arrow Functions
Quiz
Introduction to Strings in JavaScript
JavaScript - String Methods
Introduction to Array in JavaScript
JavaScript - Array Methods
Quiz
Introduction to loops in JavaScript
JavaScript - for loop
JavaScript - while loop
JavaScript - do-while loop
JavaScript - for-in loop
JavaScript - for-of loop
JavaScript - Loop Control
Quiz
JavaScript - Maps
JavaScript - Sets
Quiz
Where to Go from Here
JavaScript - Boolean

In JavaScript, the Boolean object is a wrapper for a boolean value, either true or false. Booleans are often used in conditions to decide the flow of control in programming. Understanding how to work with Boolean objects and values is fundamental in creating logical conditions and expressions that drive decision-making processes in your code.

While directly using Boolean objects is less common than using primitive boolean values, understanding how they work, especially how different values are converted to true or false, is crucial for effective JavaScript programming.

Syntax

The Boolean object can be created using the Boolean() constructor, which converts a given value to its boolean equivalent:

var trueValue = new Boolean(true); var falseValue = new Boolean(false);
  • Using the new Boolean(value); syntax, you can create a Boolean object. The value passed to the constructor determines the truthiness or falsiness of the resulting Boolean object.

Note: It's important to note that all values passed to the Boolean() constructor, except 0, NaN, "", null, undefined, and false, create an object that evaluates to true.

Methods

Here is a list of the method associated with the Boolean object.

Method NameDescription
valueOf()Returns the primitive value of a Boolean object as a boolean data type (true or false).

Example

Let's look at an example using the valueOf() method:

Javascript

. . . .
  • A Boolean object myBool is created with the value true.
  • myBool.valueOf() returns the primitive value of myBool, which is the boolean value true.

Falsy Boolean Values

In JavaScript, certain values are considered "falsy," meaning they are treated as false in Boolean contexts. These include:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN

All other values are considered "truthy" and will evaluate to true in Boolean contexts.

Example: Falsy Boolean Values

Javascript

. . . .
  • In this example, we use an if statement to test a series of values: false, 0, "" (empty string), null, undefined, and NaN.
  • The || (logical OR) operator is used to chain these values together. In a Boolean context, JavaScript evaluates each operand from left to right and stops at the first truthy value. If no truthy value is found, the last value is returned.
  • Since all these values are falsy, the condition in the if statement evaluates to false, and the code block associated with the if statement is skipped.
  • The program then executes the else block, printing "All values are falsy." to the console.
JavaScript - Number
JavaScript - Math
Mark as Completed

On this page

Syntax

Methods

Example

Falsy Boolean Values

Example: Falsy Boolean Values